package org.zky.modules.account.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.zky.modules.account.domain.ZkyAdminAccountTab;
import org.zky.modules.account.repository.ZkyAdminAccountTabRepository;
import org.zky.modules.account.service.ZkyAdminAccountTabService;
import org.zky.modules.account.service.dto.ZkyAdminAccountTabDto;
import org.zky.modules.account.service.dto.ZkyAdminAccountTabQueryCriteria;
import org.zky.modules.account.service.mapper.ZkyAdminAccountTabMapper;
import org.zky.utils.FileUtil;
import org.zky.utils.PageUtil;
import org.zky.utils.QueryHelp;
import org.zky.utils.ValidationUtil;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * @author chenglong
 * @date 2020-03-23
 */
@Service
//@CacheConfig(cacheNames = "zkyAdminAccountTab")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ZkyAdminAccountTabServiceImpl implements ZkyAdminAccountTabService {

    private final ZkyAdminAccountTabRepository zkyAdminAccountTabRepository;

    private final ZkyAdminAccountTabMapper zkyAdminAccountTabMapper;

    public ZkyAdminAccountTabServiceImpl(ZkyAdminAccountTabRepository zkyAdminAccountTabRepository, ZkyAdminAccountTabMapper zkyAdminAccountTabMapper) {
        this.zkyAdminAccountTabRepository = zkyAdminAccountTabRepository;
        this.zkyAdminAccountTabMapper = zkyAdminAccountTabMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ZkyAdminAccountTabQueryCriteria criteria, Pageable pageable){
        Page<ZkyAdminAccountTab> page = zkyAdminAccountTabRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(zkyAdminAccountTabMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ZkyAdminAccountTabDto> queryAll(ZkyAdminAccountTabQueryCriteria criteria){
        return zkyAdminAccountTabMapper.toDto(zkyAdminAccountTabRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ZkyAdminAccountTabDto findById(Long accountId) {
        ZkyAdminAccountTab zkyAdminAccountTab = zkyAdminAccountTabRepository.findById(accountId).orElseGet(ZkyAdminAccountTab::new);
        ValidationUtil.isNull(zkyAdminAccountTab.getAccountId(),"ZkyAdminAccountTab","accountId",accountId);
        return zkyAdminAccountTabMapper.toDto(zkyAdminAccountTab);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ZkyAdminAccountTabDto create(ZkyAdminAccountTab resources) {
        return zkyAdminAccountTabMapper.toDto(zkyAdminAccountTabRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ZkyAdminAccountTab resources) {
        ZkyAdminAccountTab zkyAdminAccountTab = zkyAdminAccountTabRepository.findById(resources.getAccountId()).orElseGet(ZkyAdminAccountTab::new);
        ValidationUtil.isNull( zkyAdminAccountTab.getAccountId(),"ZkyAdminAccountTab","id",resources.getAccountId());
        zkyAdminAccountTab.copy(resources);
        zkyAdminAccountTabRepository.save(zkyAdminAccountTab);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long accountId : ids) {
            zkyAdminAccountTabRepository.deleteById(accountId);
        }
    }

    @Override
    public void download(List<ZkyAdminAccountTabDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZkyAdminAccountTabDto zkyAdminAccountTab : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" accountName",  zkyAdminAccountTab.getAccountName());
            map.put("user mobile", zkyAdminAccountTab.getMobileNo());
            map.put("1:active 2:inactive", zkyAdminAccountTab.getStatus());
            map.put(" lastLoginTime",  zkyAdminAccountTab.getLastLoginTime());
            map.put("delete time", zkyAdminAccountTab.getDeleteAt());
            map.put(" createTime",  zkyAdminAccountTab.getCreateTime());
            map.put(" updateTime",  zkyAdminAccountTab.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}